<?php

/**
 * 前台 栏目 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: category.class.php 107 2012-03-14 11:40:18Z zwt007 $
 * http://www.t-mac.org；
 */
class category extends Model
{

    private $db;
    private $category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->category_table = DB_PREFIX . 'category';
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getCategoryInfo($class_id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->category_table WHERE cat_id = $class_id LIMIT 0,1");
        return $rs;
    }

    /**
     * 取分类列表
     * @global type $category_list
     * @param type $parent
     * @param type $url
     * @param type $username
     * @param type $indexurl
     * @return type 
     */
    public function getCategoryList($parent)
    {
        $catetory_array = $this->getCategoryArray();
        global $category_list;
        $category_list = '';
        $category_list = $this->fenlei($catetory_array, $parent);
        $this->assign('category_list', $category_list);
        return $category_list;
    }

    /**
     * 取出所有Category数组
     * @param type $uid
     * @return type 
     */
    public function getCategoryArray()
    {
        $sqltree = "SELECT cat_id,cat_pid,cat_name,channeltype,category_nicename,category_content FROM $this->category_table ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->getAll($sqltree);
        return $rs;
    }

    /**
     * 取无限分类
     * @global type $category_list
     * @param type $arr
     * @param type $parent
     * @param type $url
     * @param type $username
     * @param type $indexurl
     * @return string 
     */
    public function fenlei($arr, $parent)
    {
        global $category_list; //定义全局变量 返回值        
        $BASE_V = 'Public/' . APP_NAME . '/';
        $indexurl = Tmac::config('configcache.config.cfg_indexurl', APP_NAME, '.inc.php');
        $num = count($arr);

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($i == 0) {
                for ($j = 0; $j < $num; $j++) {
                    if ($arr[$j]['cat_pid'] == $parent) {
                        if ($parent == 0) {
                            $category_list .= '<ul class="menu">';
                        } else {
                            $category_list .= '<ul class="sub_menu">';
                        }
                        break;
                    }//有元素的该层开始是输出<ul>，
                }
            }
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                $category_list .= "<li><a href=\"{$indexurl}{$arr_array['category_nicename']}/\">" . $arr_array['cat_name'] . "</a>";
                $this->fenlei($arr, $arr[$i]['cat_id']);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>
                $category_list .= "</li>";      //该</li>放在fenlei函数后是为了把形成该元素的子元素也要包含在该元素的<li>内
            }
            if ($i == ($num - 1)) {
                for ($j = 0; $j < $num; $j++) {
                    if ($arr[$j]['cat_pid'] == $parent) {
                        $category_list .= "</ul>";
                        break;
                    }//闭合ul
                }
            }
        }
        return $category_list;
    }

    /**
     * 取Select list
     * @global string $category_tree_list
     * @param type $parent
     * @param type $uid
     * @param type $current_id
     * @return string 
     */
    public function getCategoryTreeList($parent, $current_id)
    {
        $catetory_array = $this->getCategoryArray();
        global $category_tree_list;
        $category_tree_list = '';
        $category_tree_list = $this->tree_fenlei($catetory_array, $parent, 0, $current_id);
        $this->assign('category_tree_list', $category_tree_list);
        return $category_tree_list;
    }

    /**
     * 递归取select option
     * @global string $category_tree_list
     * @param type $arr
     * @param type $parent
     * @param type $level
     * @param type $current_id
     * @return string 
     */
    public function tree_fenlei($arr, $parent, $level, $current_id)
    {
        global $category_tree_list; //定义全局变量 返回值        
        $BASE_V = 'Public/' . APP_NAME . '/';
        $num = count($arr);
        $prefix_space = '';
        for ($j = 0; $j < $level; $j++) {
            $prefix_space .= "　"; //第一级就增加一个缩进            
        }

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                if ((int) $arr_array['cat_id'] === (int) $current_id) {
                    $select = " selected";
                } else {
                    $select = "";
                }
                $category_tree_list .= '<option value="' . $arr_array['cat_id'] . '" ' . $select . '>' . $prefix_space . '|-' . $arr_array['cat_name'] . '</option>';
                $this->tree_fenlei($arr, $arr[$i]['cat_id'], $level + 1, $current_id);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>                
            }
        }
        return $category_tree_list;
    }

    /**
     * 取所有的子ID
     * @global type $son_tree_list
     * @param type $parent
     * @return type 
     */
    public function getSonTreeList($parent)
    {
        $catetory_array = $this->getCategoryArray();
        global $son_tree_list;
        $son_tree_list = '';
        $son_tree_list = $this->son_fenlei($catetory_array, $parent);

        if (empty($son_tree_list)) {
            $cat_id_rs = $parent;
        } else {
            $cat_id_rs = $son_tree_list . ',' . $parent;
            $cat_id_rs = substr($cat_id_rs, 1);
        }
        $this->assign('son_tree_list', $cat_id_rs);
        return $cat_id_rs;
    }

    /**
     * 取所有的子ID
     * @global type $son_tree_list
     * @param type $arr
     * @param type $parent
     * @return type 
     */
    public function son_fenlei($arr, $parent)
    {
        global $son_tree_list; //定义全局变量 返回值        
        $num = count($arr);

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                $son_tree_list .= ',' . $arr_array['cat_id'];
                $this->son_fenlei($arr, $arr[$i]['cat_id']);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>                
            }
        }

        return $son_tree_list;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getClass()
    {
        $rs = $this->db->getAll("SELECT * FROM $this->category_table ORDER BY cat_order DESC, cat_id DESC");
        return $rs;
    }

}